<template>
    <div class="app-container">
      <el-form label-width="120px" :model="classes" :rules="formRules" ref="dataForm">
        <el-form-item label="年级">
          <el-input-number v-model="classes.grade" placeholder="请输入年级" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="班级名称" prop="name">
          <el-input v-model="classes.name"/>
        </el-form-item>
        <el-form-item label="班主任教师" prop="headTeacherId">
          <el-select v-model="classes.headTeacherId" filterable clearable placeholder="请选择">
            <el-option
                v-for="option in options"
                :key="option.id"
                :label="option.name"
                :value="option.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            :disabled="saveBtnDisabled"
            type="primary"
            @click="saveOrUpdate">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>
  <script>
  import classes from '@/api/cms/classes'
  import teacher from '@/api/cms/teacher'  
  export default {
    data() {
      return {
        // 存放班级信息
        classes: {
            grade: 0,
            name: '',
            headTeacherId: ''
        },
        saveBtnDisabled: false, // 保存按钮是否禁用
        options: undefined,
        formRules: {
            name: [
                { required: true, message: '请输入姓名', trigger: 'blur' }
            ],
            headTeacherId: [
                { required: true, message: '请输入班主任信息', trigger: 'blur' }
            ]
        }
      }
    },
    watch: {
      $route(to, from) { // 监听路由变化方式
        this.init()
      }
    },
    created() {
      this.init()
      teacher.findAllTeachers().then(res=>{
        console.log(res)
        this.options = res.data.content
      }).catch(err=>{
        console.log(err)
      })
    },
    methods: {
      init() {
        // 判断是否有id
        if (this.$route.params && this.$route.params.id) {
          const id = this.$route.params.id
          this.getInfo(id)
        } else {
            // 做添加 清空表单
            this.classes =  {
                grade: 0,
                name: '',
                headTeacherId: ''
            }
        }
      },
      // 根据id查询方法
      getInfo(id) {
        console.log("********")
        classes.getInfoById(id)
          .then(response => {
            console.log(response);
            this.classes = response.data.item
          })
      },
      saveOrUpdate() {
        this.$refs.dataForm.validate(valid => {
            // console.log(valid)
            if (valid) {
                // console.log('通过')
            // 表单验证通过，执行提交操作
            this.saveBtnDisabled = true
            // 判断saveBtnDisabled是否有id 修改或添加
                if (!this.classes.id) {
                // 添加
                this.saveData()
                } else {
                this.updateData()
                }
            } else {
            // 表单验证失败，输出错误信息
            // console.log('不通过')
                this.$message.error('请正确输入表单');
            }
        })
      },
      // 修改教师方法
      updateData() {
        classes.updateInfo(this.classes)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.$router.push({ path: '/classes/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '修改失败'
            })
          })
      },
      // 保存
      saveData() {
        classes.addInfo(this.classes)
          .then(response => { // 添加成功
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            // 路由跳转
            this.$router.push({ path: '/classes/table' })
          }).catch(response => {
            this.$message({
              type: 'error',
              message: '保存失败'
            })
          })
      }
    }
  }
  </script>
  